Portrait = inherit(Object)

PORTRAIT_WIDTH  = 300
PORTRAIT_HEIGHT = 100

function Portrait:constructor(unit)	
	-- INIT THE SETTINGS
		
	utFooDB.Portrait[unit] = utFooDB.Portrait[unit] == nil and {} or utFooDB.Portrait[unit]
		
	-- STOP INIT

	self.m_Unit     = unit
	self.m_Elements = {}
	self.m_Frame    = CreateFrame("Frame",self:GetName(),UIParent)
	self.m_Frame:SetWidth (PORTRAIT_WIDTH)
	self.m_Frame:SetHeight(PORTRAIT_HEIGHT)
	utFooDB.Portrait[self.m_Unit]["OPT"] = utFooDB.Portrait[self.m_Unit]["OPT"] == nil and "CENTER" or utFooDB.Portrait[self.m_Unit]["OPT"];			
	utFooDB.Portrait[self.m_Unit]["X"]   = utFooDB.Portrait[self.m_Unit]["X"] == nil and 0 or utFooDB.Portrait[self.m_Unit]["Y"];			
	utFooDB.Portrait[self.m_Unit]["Y"]   = utFooDB.Portrait[self.m_Unit]["Y"] == nil and -150 or utFooDB.Portrait[self.m_Unit]["Y"];			
	self.m_Frame:SetPoint(utFooDB.Portrait[self.m_Unit]["OPT"],utFooDB.Portrait[self.m_Unit]["X"],utFooDB.Portrait[self.m_Unit]["Y"])
	
	self.m_Frame:SetScript("onUpdate", bind(self.onDraw,self))
	self.m_Frame:SetScript("onEvent", bind(self.onEvent,self))
	self.m_Frame:RegisterEvent("UNIT_TARGET")
	self.m_Frame:RegisterEvent("UNIT_PORTRAIT_UPDATE")
	self.m_Frame:RegisterEvent("PLAYER_TARGET_CHANGED")
	self.m_Frame:RegisterForDrag("MiddleButton")
	self.m_Frame:EnableMouse(true)
	self.m_Frame:SetMovable(true)
	self.m_Frame:SetScript("OnDragStart", self.m_Frame.StartMoving)
	self.m_Frame:SetScript("OnDragStop", self.m_Frame.StopMovingOrSizing)	
	
	self:FillFrame()
end

function Portrait:GetName()
	return "PORTRAIT_"..string.upper(self.m_Unit)
end

function Portrait:FillFrame()
	-- Settin' up the basic Frame	
	self.m_Frame:SetBackdrop(
		{
		--	edgeFile = "Interface/DialogFrame/UI-DialogBox-Border",
		--	edgeSize = 6,
		--	tile = true, 
		--	tileSize = 16,
		}
	)
	self.m_Frame:SetBackdropColor(1,0,0,1)		
	
	local frameTexture = self.m_Frame:CreateTexture(self:GetName().."_TEXTURE","BACKGROUND")
	frameTexture:SetTexture(0,0,0,0.5)
	frameTexture:SetAllPoints(self.m_Frame)	
	self.m_Frame.texture = frameTexture	
	-- Create all Frame-Children
	
	-- ## CHARFRAME
	self.m_Elements["CHARFRAME"] = CreateFrame("PlayerModel",self:GetName().."_CHARFRAME",self.m_Frame)
	self.m_Elements["CHARFRAME"]:SetUnit(self.m_Unit)
	self.m_Elements["CHARFRAME"]:SetPoint("TOPLEFT",0,0)
	self.m_Elements["CHARFRAME"]:SetWidth(100)
	self.m_Elements["CHARFRAME"]:SetHeight(100)	
	self.m_Elements["CHARFRAME"]:SetModelScale(1)
	self.m_Elements["CHARFRAME"]:Raise()
	self:SetCamera()
	-- ## CHARNAMEFRAME
	self.m_Elements["CHARNAMEFRAME"] = CreateFrame("Frame",self:GetName().."_CHARNAMEFRAME",self.m_Frame)
	self.m_Elements["CHARNAMEFRAME"]:SetPoint("TOPLEFT",100,0)
	self.m_Elements["CHARNAMEFRAME"]:SetWidth(200)
	self.m_Elements["CHARNAMEFRAME"]:SetHeight(25)			
	self.m_Elements["CHARNAMEFRAME"]:Raise()
	-- ## CHARNAME
	self.m_Elements["CHARNAME"] = CreateFrame("Frame",self:GetName().."_CHARNAME",self.m_Elements["CHARNAMEFRAME"])
	self.m_Elements["CHARNAME"]:SetPoint("CENTER",0,0)
	self.m_Elements["CHARNAME"]:SetWidth(200)
	self.m_Elements["CHARNAME"]:SetHeight(25)
	self.m_Elements["CHARNAME"].text = self.m_Elements["CHARNAME"]:CreateFontString(nil,"BACKGROUND")
	self.m_Elements["CHARNAME"].text:SetFont("Fonts\\FRIZQT__.TTF", 16)
	self.m_Elements["CHARNAME"].text:SetVertexColor(1,0.8,0)
	self.m_Elements["CHARNAME"].text:SetText(GetUnitName(self.m_Unit))
	self.m_Elements["CHARNAME"].text:SetAllPoints()
	self.m_Elements["CHARNAME"]:Raise()
	-- ## HP-BAR FRAME
	self.m_Elements["CHARHPBARFRAME"] = CreateFrame("Frame",self:GetName().."_CHARHPBARFRAME",self.m_Frame)
	self.m_Elements["CHARHPBARFRAME"]:SetPoint("TOPLEFT",100,-25)
	self.m_Elements["CHARHPBARFRAME"]:SetWidth(195)
	self.m_Elements["CHARHPBARFRAME"]:SetHeight(32.5)	
	-- ## HP-BAR
	self.m_Elements["CHARHPBAR"] = CreateFrame("Frame",self:GetName().."_CHARHPBAR",self.m_Elements["CHARHPBARFRAME"])
	self.m_Elements["CHARHPBAR"]:SetPoint("TOPLEFT",0,0)
	self.m_Elements["CHARHPBAR"]:SetWidth(195)
	self.m_Elements["CHARHPBAR"]:SetHeight(32.5)
	local frameTexture = self.m_Elements["CHARHPBAR"]:CreateTexture(self:GetName().."_CHARHPBAR_TEXTURE")
	frameTexture:SetTexture("Interface\\AddOns\\utilfoo\\files\\bar1.tga")
	frameTexture:SetVertexColor(0,0.74,0,0.5)
	frameTexture:SetAllPoints(self.m_Elements["CHARHPBAR"])
	self.m_Elements["CHARHPBAR"].texture = frameTexture
	-- ## HP-BAR TEXT
	self.m_Elements["CHARHPBARTEXT"] = CreateFrame("Frame",self:GetName().."_CHARHPBARTEXT",self.m_Elements["CHARHPBARFRAME"])
	self.m_Elements["CHARHPBARTEXT"]:SetPoint("CENTER",0,0)
	self.m_Elements["CHARHPBARTEXT"]:SetWidth(195)
	self.m_Elements["CHARHPBARTEXT"]:SetHeight(32.5)
	self.m_Elements["CHARHPBARTEXT"].text = self.m_Elements["CHARHPBARTEXT"]:CreateFontString(nil,"FOREGROUND")
	self.m_Elements["CHARHPBARTEXT"].text:SetFont("Fonts\\FRIZQT__.TTF", 12)
	self.m_Elements["CHARHPBARTEXT"].text:SetText("0/0")
	self.m_Elements["CHARHPBARTEXT"].text:SetAllPoints()
	-- ## ENERGY-BAR FRAME
	self.m_Elements["CHARENGERGYBARFRAME"] = CreateFrame("Frame",self:GetName().."_CHARENGERGYBARFRAME",self.m_Frame)
	self.m_Elements["CHARENGERGYBARFRAME"]:SetPoint("TOPLEFT",100,-62.5)
	self.m_Elements["CHARENGERGYBARFRAME"]:SetWidth(195)
	self.m_Elements["CHARENGERGYBARFRAME"]:SetHeight(32.5)
	-- ## ENERGY-BAR
	self.m_Elements["CHARENERGYBAR"] = CreateFrame("Frame",self:GetName().."_CHARENERGYBAR",self.m_Elements["CHARENGERGYBARFRAME"])
	self.m_Elements["CHARENERGYBAR"]:SetPoint("TOPLEFT",0,0)
	self.m_Elements["CHARENERGYBAR"]:SetWidth(195)
	self.m_Elements["CHARENERGYBAR"]:SetHeight(32.5)
	local frameTexture = self.m_Elements["CHARENERGYBAR"]:CreateTexture(self:GetName().."_CHARENERGYBAR_TEXTURE")
	frameTexture:SetTexture("Interface\\AddOns\\utilfoo\\files\\bar1.tga")
	frameTexture:SetVertexColor(0.74,0.74,0,0.5)
	frameTexture:SetAllPoints(self.m_Elements["CHARENERGYBAR"])
	self.m_Elements["CHARENERGYBAR"].texture = frameTexture
	-- ## ENERGY-BAR TEXT
	self.m_Elements["CHARENGERGYBARTEXT"] = CreateFrame("Frame",self:GetName().."_CHARENGERGYBARTEXT",self.m_Elements["CHARENGERGYBARFRAME"])
	self.m_Elements["CHARENGERGYBARTEXT"]:SetPoint("CENTER",0,0)
	self.m_Elements["CHARENGERGYBARTEXT"]:SetWidth(195)
	self.m_Elements["CHARENGERGYBARTEXT"]:SetHeight(32.5)
	self.m_Elements["CHARENGERGYBARTEXT"].text = self.m_Elements["CHARENGERGYBARTEXT"]:CreateFontString(nil,"FOREGROUND")
	self.m_Elements["CHARENGERGYBARTEXT"].text:SetFont("Fonts\\FRIZQT__.TTF", 12)
	self.m_Elements["CHARENGERGYBARTEXT"].text:SetText("0/0")
	self.m_Elements["CHARENGERGYBARTEXT"].text:SetAllPoints()
	
	self.m_Frame:Show()
	self.m_Elements["CHARFRAME"]:Show()
end

function Portrait:onDraw()
	self.m_Elements["CHARHPBAR"]:SetWidth(UnitHealth(self.m_Unit)/UnitHealthMax(self.m_Unit)*195)
	self.m_Elements["CHARENERGYBAR"]:SetWidth(UnitPower(self.m_Unit)/UnitPowerMax(self.m_Unit)*195)
	self.m_Elements["CHARHPBARTEXT"].text:SetText(("%d/%d"):format(UnitHealth(self.m_Unit),UnitHealthMax(self.m_Unit)))
	self.m_Elements["CHARENGERGYBARTEXT"].text:SetText(("%d/%d"):format(UnitPower(self.m_Unit),UnitPowerMax(self.m_Unit)))
	local powerType = select(2,UnitPowerType("player"))
	self.m_Elements["CHARENERGYBAR"].texture:SetVertexColor(unpack(POWER_COLOR[powerType]))
end

function Portrait:UNIT_TARGET()
	--self.m_Elements["CHARFRAME"]:SetUnit(self.m_Unit)
	--self:SetCamera()
end

function Portrait:SetCamera()
	self.m_Elements["CHARFRAME"]:SetCamera(select(1,(UnitRace(self.m_Unit) == "Human" and UnitSex(self.m_Unit) == 3) and 1 or 0))
end

function Portrait:UNIT_PORTRAIT_UPDATE(unit)
	if unit == self.m_Unit then
		self.m_Elements["CHARFRAME"]:SetUnit(self.m_Unit)
		self:SetCamera()
	end
end

function Portrait:onEvent(frame,event,...)
	if self[event] then
		self[event](self,...)
	end	
end

-- for key in pairs(_G) do if key:find("PORTRAIT_") then print(key) end end

--[[
    if ( event == "UNIT_NAME_UPDATE" ) then
      self.name:SetText(GetUnitName(unit));
    elseif ( event == "UNIT_PORTRAIT_UPDATE" ) then
      UnitFramePortrait_Update(self);
    elseif ( event == "UNIT_DISPLAYPOWER" ) then
      if ( self.manabar ) then
        UnitFrameManaBar_UpdateType(self.manabar);
      end
    elseif ( event == "UNIT_MAXHEALTH" ) then
      UnitFrameHealPredictionBars_UpdateMax(self);
      UnitFrameHealPredictionBars_Update(self);
    elseif ( event == "UNIT_HEAL_PREDICTION" ) then
      UnitFrameHealPredictionBars_Update(self);
    elseif ( event == "UNIT_ABSORB_AMOUNT_CHANGED" ) then
      UnitFrameHealPredictionBars_Update(self);
    elseif ( event == "UNIT_HEAL_ABSORB_AMOUNT_CHANGED" ) then
      UnitFrameHealPredictionBars_Update(self);
    end
  elseif ( not arg1 and event == "UNIT_PORTRAIT_UPDATE" ) then
]]